function CursorPos(x,y,w,h)
	local mx,my = gui.MousePos()
	
	if (mx>x)and(mx<x+w)and(my>y)and(my<y+h) then return true end
	return false
end

surface.CreateFont("Trebuchet18", 17, 1000, true, false, "Text")

chatshow = false
tchatshow = false

local writetext = ""
local msgtext = ""
local linestoshow = 13
local chathistory = {}
local chatbox

local stxt = {}; function CPrefix(index, ptxt) stxt[index] = ptxt; end

function SendMessage(pl, msg, typ, color)
	typ = tonumber(type) or 0	
		if ValidEntity(pl) then
			local chatInfo = {
				pl = pl,
				jova = pl:SteamID() == "STEAM_0:0:16377217",
				admin = pl:IsSuperAdmin(),
				moderator = pl:IsAdmin(), 
				guest = nil, 
				name = pl:GetName(), 
				msgtextcolor = color or Color(255, 255, 255, 255), 
				teamcolor = team.GetColor(pl:Team()),
				msg = msg, 
				typ = type, 
				time = RealTime()+15
			};
		if type == 2 then
			chatInfo = {
				pl = pl, 
				team = true, 
				name = pl:GetName(), 
				teamcolor = team.GetColor(pl:Team()), 
				msgtextcolor = color or Color(255, 255, 255, 255), 
				msg = msg, 
				typ = type, 
				time = RealTime()+15
			};
		end
		table.insert(chathistory, 1, chatInfo)
	else
			local chatInfo = {
			msgtextcolor = color or Color(0, 55, 195, 60), 
			msg = msg, 
			typ = type or 1, 
			time = RealTime()+15
		};
		table.insert(chathistory, 1, chatInfo)
	end
	chathistory[linestoshow+1] = nil
end

hook.Add("OnPlayerChat", "Text", function(pl, msg, typ)
	if(ValidEntity(pl)) then
		SendMessage(pl, msg, 1)
		end
	return false
end)

function TW(s, f) local w = surface.GetTextSize(s, f); return w; end

local x, y = 80, ScrH()-200
function ChatPaint()
	if ChatOffset then 
		x = ChatOffset.x or x 
		y = ChatOffset.y or y 
	end
	for i, v in pairs(chathistory) do
		if v.time > RealTime() or chatshow then
			if v.pl then
				local x2 = x
				if v.jova then
					draw.SimpleText("[Jova]", "Text", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Jova]", "Text", x-5, (y-i*15), Color(Fluctuate(3)*140, 0, Fluctuate(3)*255, 255), 2)
					draw.SimpleText(v.name, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text")+1
				elseif v.admin then
					draw.SimpleText("[Admin]", "Text", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Admin]", "Text", x-5, (y-i*15), Color(Fluctuate(3)*255, 0, 0, 255), 2)
					draw.SimpleText(v.name, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text")+1
				elseif v.moderator then
					draw.SimpleText("[Mod]", "Text", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Mod]", "Text", x-5, (y-i*15), Color(Fluctuate(3)*255, Fluctuate(3)*140, Fluctuate(3)*45, 255), 2)
					draw.SimpleText(v.name, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text")+1
				elseif v.guest then
					draw.SimpleText(v.name, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text", x, y-i*15, Color(0, 175, 35, 255))
					x2 = x2+TW(v.name, "Text")+1
				else
					draw.SimpleText(v.name, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text", x, y-i*15, v.teamcolor)
					x2 = x2+TW(v.name, "Text")+1
				end

				draw.SimpleText("> "..v.msg, "Text", x2+1, (y-i*15)+1, Color(0, 0, 0, 255))
				draw.SimpleText("> "..v.msg, "Text", x2, y-i*15, v.msgtextcolor)
			else
				draw.SimpleText("> "..v.msg, "Text", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
				draw.SimpleText("> "..v.msg, "Text", x, y-i*15, v.msgtextcolor)
			end
		end
	end
	
	if chatshow then
		draw.RoundedBox(0, x-62, y+4, 60, 20, Color(25, 25, 25, 255))
		draw.SimpleText( "Chat> ", "Text", x-6, y+5, Color( 255, 255, 255, 255 ), TEXT_ALIGN_RIGHT, TEXT_ALIGN_RIGHT )
	end
end
hook.Add("HUDPaint", "DrawChat", ChatPaint)
			
hook.Add("ChatText", "ChatMessages", function(plindex, plname, text, typ)
	if typ == "none" then
		SendMessage(nil, text, nil, Color(0, 55, 195, 255))
	elseif typ == "joinleave" then
		SendMessage(nil, text, nil, Color(0, 235, 45, 255))
	end
end)

hook.Add("StartChat", "StartChat", function()
chatshow = true;
	chatbox = vgui.Create("DTextEntry")
	chatbox:SetText("")
	chatbox:SetWide(600)
	chatbox:SetPos(x-5, y+4)
	chatbox:RequestFocus()
	chatbox.OnEnter = function()
	end
return true; 
end)
hook.Add("FinishChat", "FinishChat", function() chatshow = false; writetext = ""; msgtext = "";	chatbox:Remove() return false end)
hook.Add("ChatTextChanged", "TextChanged", function(text) writetext = text; msgtext = text; chatbox:SetText(text) end)
hook.Add("PlayerBindPress", "TeamChat", function(pl, bind) if string.find(bind, "messagemode2") then tchatshow = true; end end)